package com.ly.hotel.view.setting;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.DeviceInfo;
import com.ly.hotel.model.ModelBean;
import com.ly.hotel.service.SPAService;
import com.ly.hotel.util.HttpRequestFailException;
import com.ly.hotel.view.common.PullDownMenu;
import com.ly.hotel.view.common.PullDownMenu.SelectListner;

/**
 * 设置捷信达服务器ip
 * @author Administrator
 *
 */
public class ChangeMacSettingLayout extends LinearLayout {
	
	public static final int UPDATE_GROUP_DATA = 1;
	public static final int UPDATE_DEV_DATA = 2;
	public static final int SHOW_LOG = 3;
	private PullDownMenu groupName;
	private PullDownMenu deviceName;
	private List<String> group_data_list;
	private List<String> device_data_list;
	private Button okBtn;
	private Context context;
	private TextView info;
	private HashMap<String, String> mydevMacMap = new HashMap<String, String>();
	private ThisHandler handlerMsg = new ThisHandler(this);
	
	public ChangeMacSettingLayout(Context context) {
		super(context);
		this.context = context;
		LayoutInflater.from(getContext()).inflate(R.layout.layout_change_mac, this);
		initView();
		
		getGroup();
		getDevice();
		
	}

	private void initView() {
		
		groupName = (PullDownMenu) findViewById(R.id.groupName);
		deviceName = (PullDownMenu) findViewById(R.id.deviceName);
		okBtn = (Button) findViewById(R.id.okBtn);
		info = (TextView) findViewById(R.id.testInfoTxt);
		okBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				info.append("修改座位号(" + groupName.getText());
				info.append("-" + deviceName.getText() + ")对应MAC地址\r\n");
				info.append(mydevMacMap.get(deviceName.getText()) + "=>" + MyApplication.getMAC() + "\r\n");
				Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
				updateMac(mydevMacMap.get(deviceName.getText()), MyApplication.getMAC());
			}
		});

	}
	
	private void getGroup(){
		//数据
		group_data_list = new ArrayList<String>();
//		group_data_list.add("休息厅");
//        group_data_list.add("VIP包房");
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<String> groups = null;
//				info.append("获取设备类别\r\n");
				
				try {
					groups = SPAService.getAllGroup();
					
					if (groups != null) {
						group_data_list.addAll(groups);
						handlerMsg.sendEmptyMessage(UPDATE_GROUP_DATA);
					}else{
						
					}
				} catch (HttpRequestFailException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		
		groupName.setOnSelectListner(new SelectListner() {
			
			@Override
			public void onWorkListner(final String name) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						List<DeviceInfo> devInfo = null;
//						info.append("获取" + name + "类别下设备列表\r\n");
						try {
							devInfo = SPAService.getGroupDevice(name.split(",")[1]);
						} catch (HttpRequestFailException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						if (devInfo != null) {
							device_data_list.clear();
							mydevMacMap.clear();
							for(DeviceInfo devinfo : devInfo){
								mydevMacMap.put(devinfo.getName(), devinfo.getMac());
								device_data_list.add(devinfo.getName());
							}
//							device_data_list.addAll(groups);
//							deviceName.setData("位置号", group_data_list, false);
							handlerMsg.sendEmptyMessage(UPDATE_DEV_DATA);
						}else{
							
						}
					}
				}).start();
			}
		});
	}

	private void updateMac(final String oldMac, final String newMac){
		new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try {
							ModelBean mb;
							Message msg = new Message();
							Bundle bundle = new Bundle();
							mb = SPAService.getUpdateMac(oldMac, newMac);
							
							msg.what = SHOW_LOG;
							bundle.putString("log", mb.getMsg());
							msg.setData(bundle);
							handlerMsg.sendMessage(msg);
							
						} catch (HttpRequestFailException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}).start();
	}
	private void getDevice(){
		//数据
		device_data_list = new ArrayList<String>();

		deviceName.setData("位置号", device_data_list, false);
		deviceName.setOnSelectListner(new SelectListner() {
			
			@Override
			public void onWorkListner(String name) {
				Log.i("changeMac", "选中设备名:" + name);
			}
		});
	}

	private static class ThisHandler extends Handler {
		private WeakReference<ChangeMacSettingLayout> wrActivity;

		public ThisHandler(ChangeMacSettingLayout activity) {
			wrActivity = new WeakReference<ChangeMacSettingLayout>(activity);
		}
   
		@Override
		public void handleMessage(Message message) {
			ChangeMacSettingLayout act = wrActivity.get(); 
			
			if (act != null) {
				switch (message.what) {
				case UPDATE_GROUP_DATA:
					act.groupName.setData("分组", act.group_data_list, false);
					break;
				case UPDATE_DEV_DATA:
					act.deviceName.setData("位置号", act.device_data_list, false);
					break;
				
				case SHOW_LOG:
					act.info.append(message.getData().getString("log") + "\r\n");
					break;
				}
			}
		}
	}
}
